<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2024/11/22 0022
 * Time: 15:13
 */

namespace app\index\controller;

use app\index\model\School as schoolModel;
use app\index\model\Classes as classesModel;
use app\index\model\Course as courseModel;
use app\index\model\Score as scoreModel;

class Student extends Base
{
    private $model;
    public function initialize()
    {
        parent::initialize();
        $this->model = new \app\index\model\Student();
    }

    public function index()
    {
        return $this->fetch();
    }

    public function lst()
    {
        $start = $this->request->get('start', 0);
        $length  = $this->request->get('length', 15);
        $search  = $this->request->get('keyword');
        $where = [];
        if ($search) {
            $where[] = ['t.name', 'like', "%{$search}%"];
        }
        $data = $this->model->getList($start, $length, $where);
        $total = $this->model->getListCount($where);
        $result = [
            'draw' => $this->request->get('draw'),
            'recordsTotal' => $total,
            'recordsFiltered' => $total,
            'data' => $data
        ];
        return json($result);
    }

    public function add()
    {
        if ($this->request->isAjax()) {
            $data = $this->request->post('data/a', []);
            $result = $this->validate($data, 'app\index\validate\Student');
            if ($result !== true) {
                $this->error($result);
            }
            $res = $this->model->add($data);
            if ($res !== false) {
                $this->success('添加成功');
            }
            $this->error('添加失败');
        }
        $schoolModel = new schoolModel();
        $school = $schoolModel->getAll();
        $classesModel = new classesModel();
        $class = $classesModel->getAll();
        $this->assign('school', $school);
        $this->assign('class', $class);
        return $this->fetch();
    }

    public function edit()
    {
        if ($this->request->isAjax()) {
            $data = $this->request->post('data/a', []);
            $id = $this->request->post('id', 0);
            $result = $this->validate($data, 'app\index\validate\Student');
            if ($result !== true) {
                $this->error($result);
            }
            $res = $this->model->edit($id, $data);
            if ($res !== false) {
                $this->success('编辑成功');
            }
            $this->error('编辑失败');
        }
        $id = $this->request->get('id');
        $student = $this->model->getOneById($id);
        $this->assign('student', $student);

        $schoolModel = new schoolModel();
        $school = $schoolModel->getAll();
        $classesModel = new classesModel();
        $class = $classesModel->getAll();
        $this->assign('school', $school);
        $this->assign('class', $class);
        return $this->fetch();
    }

    public function del()
    {
        if ($this->request->isAjax()) {
            $id = $this->request->get('id');
            $res = $this->model->del($id);
            if ($res !== false) {
                $this->success('删除成功');
            }
            $this->error('删除失败');
        }
    }

    public function look()
    {
        $id = $this->request->get('id');
        $class_id = $this->request->get('class_id');

        //获取所有年级的年级课程的成绩列表
        $student = $this->model->getOneById($id);
        if (!$student) {
            $this->error('学生不存在');
        }
        if (!isset($class_id)) {
            $class_id = $student['class_id'];
        }
        $class =  classesModel::select()->toArray();
        $course = courseModel::select()->toArray();
        $list = $labels = array();

        //获取学生的成绩时间
        $labels = scoreModel::where([['stu_id', '=', $id],['class_id', '=', $class_id]])->distinct(true)->order("time", "asc")->column('time');

        $borderColor = ['#FF6384', '#36A2EB', '#4BC0C0', '#FF9F40'];

        foreach ($course as $ks => $vs) {
            $data = array();
            foreach ($labels as $i => $time) {
                $score = scoreModel::where([['stu_id', '=', $id],['class_id', '=', $class_id], ['course_id', '=', $vs['id']], ['time', '=', $time]])->find();
                $data[] = $score ? $score['score'] : 0;
            }

            $list[$ks] = array(
                'label' => $vs['name'],
                'borderColor' => $borderColor[$ks],
                'backgroundColor' => $borderColor[$ks],
                'data' => $data
            );
        }

        $this->assign('data', json_encode($list));
        $this->assign('labels', json_encode($labels));
        $this->assign('class', $class);
        $this->assign('class_id', $class_id);
        $this->assign('id', $id);
        return $this->fetch();
    }

    public function more()
    {
        $id = $this->request->get('id');
        $class_id = $this->request->get('class_id');
        $course_id = $this->request->get('course_id');
        $student = $this->model->getOneById($id);
        if (!$student) {
            $this->error('学生不存在');
        }
        if (!isset($class_id)) {
            $class_id = $student['class_id'];
        }
        $class =  classesModel::select()->toArray();
        $course = courseModel::select()->toArray();
        $this->assign('class', $class);
        $this->assign('course', $course);
        $this->assign('class_id', $class_id);
        $this->assign('id', $id);
        $this->assign('course_id', $course_id ? $course_id : 0);
        $list = $labels = array();
        if ($course_id) {
            //获取学生的成绩时间
            $labels = scoreModel::where([['stu_id', '=', $id],['class_id', '=', $class_id], ['course_id', '=', $course_id]])
                ->distinct(true)
                ->order("time", "asc")
                ->column('time');
            $data = array();
            foreach ($labels as $i => $time) {
                $score = scoreModel::where([['stu_id', '=', $id],['class_id', '=', $class_id], ['course_id', '=', $course_id], ['time', '=', $time]])->find();
                $data[] = $score ? $score['score'] : 0;
            }
            $course_info = courseModel::get($course_id);
            $list[] = array(
                'label' => $course_info['name'],
                'borderColor' => '#FF6384',
//                'backgroundColor' => '#FF6384',
                'data' => $data
            );
        }
        $this->assign('data', json_encode($list));
        $this->assign('labels', json_encode($labels));
        return $this->fetch();
    }
}